// Learn cc.Class:
//  - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html
import global from './global'
import axios from 'axios'
cc.Class({
    extends: cc.Component,

    properties: {
        fruit: {
            type: cc.Prefab,
            default: null
        },
        atlas: {
            type: cc.SpriteAtlas,
            default: null
        },
        canClick: {
            type: cc.Boolean,
            default: null
        }
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        cc.director.getPhysicsManager().enabled = true;
        cc.director.getPhysicsManager().gravity = cc.v2(0, -500);
        this.canClick = true;
        this.node.on('addScore',this.addScore,this);

        this.node.on(cc.Node.EventType.TOUCH_START, this.watermelonDown, this);
        
      this.getData();
    },
    watermelonDown(event) {
        if(!this.canClick) return;
        this.canClick = false;
        this.scheduleOnce(_=>{
            this.canClick = true;
        }, 1)
        let pos = this.node.convertToNodeSpaceAR(event.getLocation());
        let random = Math.floor(Math.random() * 5);
        global.FruitType = random;
        let watermelon = cc.instantiate(this.fruit);
        watermelon.active = true;
        watermelon.setPosition(cc.v2(pos.x, 400))
        this.node.addChild(watermelon);
    },
    addScore(score){
        let scoreNode = cc.find("Canvas/bg/score");
        scoreNode.getComponent(cc.Label).string = global.SCORE.toString();

  },
  getData () {
    axios.get('/user?ID=12345').then(res => {
      console.log(res)
    })
  }
});
